//
//  EditAddressInfoVC.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/24.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "EditAddressInfoVC.h"
#import "EditAddressModel.h"
#import "AddressInfoInputCell.h"
#import "AddressInfoSelectCell.h"
#import "AddressSelectView.h"

@interface EditAddressInfoVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSDictionary *cityData;//用于存放省市区数据
@end

@implementation EditAddressInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.addressModel?@"编辑收货地址":@"新增收货地址";
    self.addressModel = self.addressModel?self.addressModel:[AddressListModel new];
    //组装省市区数据
    [self createCityData];
    //配置数据
    [self configData];
    //创建tableView
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

#pragma mark - 保存按钮
-(void)saveAction{
    for (EditAddressModel *model in self.dataSource) {
        model.dataModel = self.addressModel;
        NSString *content = [model.dataModel valueForKey:model.key];
        if ([content length] == 0) {
            NSLog(@"请完善%@!",model.title);
            return;
        }else if(model.isPhone && !content.isPhone){
            NSLog(@"手机号不正确");
            return;
        }
    }
    if (self.refreshModel) {
        self.refreshModel(self.addressModel);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 懒加载
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[AddressInfoInputCell class] forCellReuseIdentifier:@"AddressInfoInputCell"];
        [_tableView registerClass:[AddressInfoSelectCell class] forCellReuseIdentifier:@"AddressInfoSelectCell"];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _tableView.backgroundColor = [UIColor lightGrayColor];
        
        UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kAppWidth_Mini(360))];
        _tableView.tableFooterView = footerView;
        UIButton *addButton = [UIButton initWithTitleStr:@"保存" titleColor:[UIColor whiteColor] withFont:UIPFThinFont(15)];
        [addButton addTarget:self action:@selector(saveAction) forControlEvents:UIControlEventTouchUpInside];
        addButton.backgroundColor = [UIColor blackColor];
        [footerView addSubview:addButton];
        [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(kAppWidth_Mini(830));
            make.height.mas_equalTo(kAppWidth_Mini(130));
            make.centerX.centerY.equalTo(footerView);
        }];
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EditAddressModel *model = self.dataSource[indexPath.row];
    model.dataModel = self.addressModel;
    if (model.type == AddressCellTypeInput){
        AddressInfoInputCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressInfoInputCell"];
        cell.dataModel = model;
        return cell;
    }else if(model.type == AddressCellTypeArrowSelect){
        AddressInfoSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressInfoSelectCell"];
        cell.dataModel = model;
        return cell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    EditAddressModel *model = self.dataSource[indexPath.row];
    return model.cellHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *bgView = [[UIView alloc]init];
    return bgView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    EditAddressModel *model = self.dataSource[indexPath.row];
    if ([model.title isEqualToString:@"所在地区"]) {
        [self showProviceCityPickerWithIndexPath:indexPath];
    }
}

#pragma mark - 省市区
-(void)showProviceCityPickerWithIndexPath:(NSIndexPath *)indexPath{
    [AddressSelectView showPickerWithDataDic:self.cityData SelectModel:self.addressModel complete:^{
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];
}

#pragma mark - 省市区数据
-(void)createCityData{
    self.cityData = [AppTools readLocalFileWithName:@"city"];
}

#pragma mark - 配置数据
-(void)configData{
    self.dataSource = [NSMutableArray array];
    EditAddressModel *model0 = [EditAddressModel new];
    model0.type = AddressCellTypeInput;
    model0.title = @"收货人";
    model0.placeHolder = @"输入姓名";
    model0.cellHeight = kAppWidth_Mini(160);
    model0.key = @"consignee";
    model0.controlLength = 10;
    [self.dataSource addObject:model0];
    
    EditAddressModel *model1 = [EditAddressModel new];
    model1.type = AddressCellTypeInput;
    model1.title = @"手机号码";
    model1.isPhone = YES;
    model1.keyboardType = KeyBoardTypeNumber;
    model1.placeHolder = @"联系人手机号码";
    model1.controlLength = 11;
    model1.cellHeight = kAppWidth_Mini(160);
    model1.key = @"mobile";
    [self.dataSource addObject:model1];
    
    EditAddressModel *model2 = [EditAddressModel new];
    model2.type = AddressCellTypeArrowSelect;
    model2.title = @"所在地区";
    model2.placeHolder = @"地区信息";
    model2.cellHeight = kAppWidth_Mini(160);
    model2.key = @"addressDesc";
    [self.dataSource addObject:model2];
    
    EditAddressModel *model3 = [EditAddressModel new];
    model3.type = AddressCellTypeInput;
    model3.title = @"详细地址";
    model3.placeHolder = @"街道门牌信息";
    model3.cellHeight = kAppWidth_Mini(160);
    model3.controlLength = 60;
    model3.key = @"address";
    [self.dataSource addObject:model3];
    
}

@end
